// pages/me/me.js
import { User } from "../../models/me-edit";
const getUser = new User();
import { check } from "../../util/identityCheck";
import { Resume } from "../../models/resume";
import { Bidding } from "../../models/bidding";
import { RedPacket } from "../../models/redpacket";
import { Me } from "../../models/me";
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
let resumeModel = new Resume();
let biddingModel = new Bidding();
let packetModel = new RedPacket();
let meModel = new Me();

Page({
  data: {
    avatar: "",
    attentionNumber: 1,
    dynamicNumber: 1,
    fansNumber: 1,
    psz: "",
    address: "",
    haveLogin: true
  },
  onShow() {
    this.someuser();
    this.mation();
    this.getTabBar().init();
    if (!check()) return;
    this.setData({
      userInfo: {
        nick_name: wx.getStorageSync("userInfo").nick_name,
        avatar: wx.getStorageSync("userInfo").avatar
      }
    });
  },
  onload() {},
  /**
   * 获取动态关注数据
   */
  mation() {
    meModel.mation(res => {
      this.setData({
        attentionNumber: res.attentionNumber,
        dynamicNumber: res.dynamicNumber,
        fansNumber: res.fansNumber
      });
    });
  },

  quitLogin() {
    this.clearStorage();
    wx.navigateTo({
      url: `/pages/login/login`
    });
  },

  toLogin() {
    wx.navigateTo({
      url: `/pages/login/login`
    });
  },

  onTabItemTap() {
    if (!wx.getStorageSync("userInfo")) {
      wx.navigateTo({
        url: `/pages/login/login`
      });
    }
  },

  getReceivedResume() {
    // 获取收到简历角标
    resumeModel.getReceivedResume(
      {
        isRead: 0,
        page: 1,
        count: 20
      },
      res => {
        this.setData({
          "pages[5].badge": res.total
        });
      }
    );
  },

  getReceivedRecruit() {
    // 获取收到招聘角标
    resumeModel.getReceivedRecruit(
      {
        isRead: 0,
        page: 1,
        count: 20
      },
      res => {
        this.setData({
          "pages[4].badge": res.total
        });
      }
    );
  },

  getReceivedBidding() {
    // 获取收到投标角标
    biddingModel.getReceivedBidding(
      {
        isRead: 0,
        page: 1,
        count: 20
      },
      res => {
        this.setData({
          "pages[6].badge": res.total
        });
      }
    );
  },

  logout() {
    this.clearStorage();
    wx.navigateTo({
      url: "/pages/login/login"
    });
  },

  clearStorage() {
    wx.removeStorageSync("address");
    wx.removeStorageSync("shareCode");
    wx.removeStorageSync("userToken");
    wx.removeStorageSync("userInfo");
    wx.removeStorageSync("showHomeDialog");
    wx.removeStorageSync("biddingData");
    wx.removeStorageSync("buyShopData");
    wx.removeStorageSync("recruitData");
    wx.removeStorageSync("sellShopData");
    wx.removeStorageSync("supplyData");
    wx.removeStorageSync("resumeData");
    wx.removeStorageSync("otherId");
    wx.removeStorageSync("otherShareTime");
  },

  getHasResume() {
    resumeModel.resumeDetail(res => {
      this.setData({
        hasResume: !!res
      });
    });
  },

  toRedpacket() {
    wx.navigateTo({
      url: "/packageA/pages/my-redpacket/my-redpacket"
    });
  },

  getMyPacket() {
    packetModel.getMyPacket(res => {
      this.setData({
        packet: res
      });
    });
  },

  getPacketRuleByName() {
    packetModel.getPacketRuleByName(
      {
        modeName: "求职"
      },
      res => {
        this.setData({
          packetRule: res
        });
      }
    );
  },
  someuser() {
    getUser.someuser(res => {
      this.setData({
        id: res && res.id,
        avatar: res && res.avatar
      });
    });
  },
  showPopup() {
    // 显示金币弹窗
    this.setData({
      halfPopup: true
    });
  },

  getGold(e) {
    if (this.data.hasResume) {
      // 有简历不弹发布简历弹窗
      Dialog.confirm({
        message: `技师发布简历后奖${this.data.packetRule.passAuditGold}金币+${this.data.packetRule.totalMoney}元现金，查看1条招聘信息奖${this.data.packetRule.browseGold}金币+${this.data.packetRule.browseMoney}元红包（金币可兑换品牌化妆品）`,
        confirmButtonText: "查看招聘"
      })
        .then(() => {
          wx.switchTab({
            url: "/pages/recruit/recruit"
          });
        })
        .catch(() => {
          // on cancel
        });
    } else {
      Dialog.confirm({
        message: `技师发布简历后奖${this.data.packetRule.passAuditGold}金币+${this.data.packetRule.totalMoney}元现金，查看1条招聘信息奖${this.data.packetRule.browseGold}金币+${this.data.packetRule.browseMoney}元红包（金币可兑换品牌化妆品）`,
        confirmButtonText: "查看招聘",
        cancelButtonText: "发布简历"
      })
        .then(() => {
          wx.switchTab({
            url: "/pages/recruit/recruit"
          });
        })
        .catch(() => {
          wx.navigateTo({
            url: "/pages/resume/resume"
          });
        });
    }
  },

  sendMsg() {
    wx.requestSubscribeMessage({
      tmplIds: [
        "b7FIjt52cUMvTClpMJKJLe8lzi4rnQsWlUc7D5VYGoA",
        "-DHKrW9DRvbg3sEoVRYtcRxRyr-kYzyTN-9BNCY89rI"
      ]
    });
  },

  toStore() {
    wx.navigateToMiniProgram({
      appId: "wxfe951dcab1dc4b81",
      path: "/pages/todayHotStyle/index"
    });
  }
});
